#!/bin/bash

#程序:
#	本程序从 SOURCES_MIRRORS_FILE 获取最快的 ubuntu 软件源
#	and backup && update /etc/apt/sources.list

#作者:  KJlmfe

#历史:
#	2012/12/6	KJlmfe 首次发布

suerver=$(lsb_release -a)
echo -e "\n$suerver\n"

#${string##*chars}
#从 string 字符串最后一次出现 *chars 的位置开始，截取 *chars 右边的所有字符。
cname=${suerver##*Codename:}

#cnver=$(${cname// /})
#cnver=$("$cname" | awk '{gsub(/ /,"")}1')
cnver=`echo $cname | sed 's/[[:space:]]//g'`

echo -e "\n确认你的系统代号是 $cnver 嘛? [y/n] \c"	
read choice

if [ "$choice" != "y" ]; then
	exit 0
fi

VERSION=$cnver  # precise 是 Ubuntu 12.04 的代码如果你的 ubuntu 不是 12.04 请更改
#echo "$VERSION"

TEST_NETCONNECT_HOST="www.baidu.com"
SOURCES_MIRRORS_FILE="sources_mirrors.list"	
MIRRORS_SPEED_FILE="mirrors_speed.list"

function get_ping_speed()	#返回平均 ping $1 次
{
	local speed=`ping -W1 -c1 $1 2> /dev/null | grep "^rtt" |  cut -d '/' -f5`
	echo $speed
}

function test_mirror_speed()	#
{
	rm $MIRRORS_SPEED_FILE 2> /dev/null; touch $MIRRORS_SPEED_FILE

	cat $SOURCES_MIRRORS_FILE | while read mirror
do
	if [ "$mirror" != "" ]; then
		echo -e "Ping $mirror \c"
		local mirror_host=`echo $mirror | cut -d '/' -f3`	#将 mirror_url 更改为主机

		local speed=$(get_ping_speed $mirror_host)

		if [ "$speed" != "" ]; then
			echo "Time is $speed"
			echo "$mirror $speed" >> $MIRRORS_SPEED_FILE
		else
			echo "Connected failed."
		fi
	fi
done
}

function get_fast_mirror()
{
	sort -k 2 -n -o $MIRRORS_SPEED_FILE $MIRRORS_SPEED_FILE
	local fast_mirror=`head -n 1 $MIRRORS_SPEED_FILE | cut -d ' ' -f1`
	echo $fast_mirror
}

function backup_sources()
{
	echo -e "备份你的sources.list.\n"
	sudo mv /etc/apt/sources.list /etc/apt/sources.list.`date +%F-%R:%S`
}

function update_sources()
{
	local COMP="main restricted universe multiverse"
	local mirror="$1"
	local tmp=$(mktemp) 

	echo "deb $mirror $VERSION $COMP" >> $tmp
	echo "deb $mirror $VERSION-updates $COMP" >> $tmp
	echo "deb $mirror $VERSION-backports $COMP" >> $tmp 
	echo "deb $mirror $VERSION-security $COMP" >> $tmp
	echo "deb $mirror $VERSION-proposed $COMP" >> $tmp

	echo "deb-src $mirror $VERSION $COMP" >> $tmp 
	echo "deb-src $mirror $VERSION-updates $COMP" >> $tmp 
	echo "deb-src $mirror $VERSION-backports $COMP" >> $tmp 
	echo "deb-src $mirror $VERSION-security $COMP" >> $tmp 
	echo "deb-src $mirror $VERSION-proposed $COMP" >> $tmp

	sudo mv "$tmp" /etc/apt/sources.list
	echo -e "您的源已更新，也许您想现在运行 \"sudo apt-get update\" .\n";
}

echo -e "\n测试网络连接.\n请稍等...   \c"

if [ "$(get_ping_speed $TEST_NETCONNECT_HOST)" == "" ]; then
	echo -e "网络连接不正常.\n请检查您的网络."; exit 1
else
	echo -e "网络连接正常.\n"
	test -f $SOURCES_MIRRORS_FILE

	if [ "$?" != "0" ]; then  
		echo -e "$SOURCES_MIRRORS_FILE 不存在.\n"; exit 2
	else
		test_mirror_speed
		fast_mirror=$(get_fast_mirror)

		if [ "$fast_mirror" == "" ]; then
			echo -e "找不到最快的软件源. 请检查您的 $SOURCES_MIRRORS_FILE\n"
			exit 0
		fi

		echo -e "\n$fast_mirror 是最快的软件源,现在替换嘛? [y/n] \c"	
		read choice

		if [ "$choice" != "y" ]; then
			exit 0
		fi

		backup_sources
		update_sources $fast_mirror
	fi
fi

exit 0
